﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.App
{
    public class HttpRequest
    {
        public string Url { get; set; }
        public Uri Uri => new Uri(Url);
        public string Path => Uri.AbsolutePath;
        public string Method { get; set; }
        public HttpRequest(string url, string method)
        {
            Url = url;
            Method = method;
        }
    }
    public class HttpResponse
    {
        private readonly MemoryStream _buffer = new MemoryStream();

        public bool HasStartd { get; private set; }

        public int StatusCode { get; set; } = 200;

        public async Task WriteAsync(byte[] bytes)
        {
            HasStartd = true;
            await _buffer.WriteAsync(bytes);
        }

        public async Task WriteAsync(string content)
        {
            await WriteAsync(Encoding.UTF8.GetBytes(content));
        }

        public byte[] GeteBuffer()
        {
            var result = new 
            {
                StatusCode,
                Content = Encoding.UTF8.GetString(_buffer.ToArray())
            };
            return JsonSerializer.SerializeToUtf8Bytes(result);
        }
    }
   
    public class HttpContext
    {
        public HttpRequest Request { get;}        
        public HttpResponse Response { get;  } = new HttpResponse();
        public IServiceProvider Services { get; }
        public IFeatureCollection Features { get; } = new FeatureCollection();

        public HttpContext(
            HttpRequest request,
            IServiceProvider services)
        {
            Request = request;
            Services = services;
        }
    }
}
